#include "QQNavigationBar.h"

QQNavigationBar::QQNavigationBar(QWidget* parent)
	: QWidget(parent)
{
	this->setFixedWidth(60);

	mUserButton = new ElaIconButton(QPixmap(), this);
	mUserButton->setFixedSize(60, 60);
	mUserButton->setBorderRadius(30);

	mNavigationModel = new QQNavigationModel(this);
	mNavigationView = new QQNavigationView(this);
	mNavigationView->setModel(mNavigationModel);
	mNavigationDelegate = new QQNavigationDelegate(this);
	mNavigationView->setItemDelegate(mNavigationDelegate);

	mFooterModel = new QQNavigationModel(this);
	mFooterView = new QQNavigationView(this);
	mFooterView->setModel(mFooterModel);
	mFooterDelegate = new QQNavigationDelegate(this);
	mFooterView->setItemDelegate(mFooterDelegate);

	QVBoxLayout* layout = new QVBoxLayout(this);
	layout->setAlignment(Qt::AlignLeft);
	layout->setSpacing(0);
	layout->setContentsMargins(0, 10, 0, 10);
	layout->addWidget(mUserButton);
	layout->addSpacing(10);
	layout->addWidget(mNavigationView);
	layout->addStretch();
	layout->addWidget(mFooterView);

	connect(mUserButton, &ElaIconButton::clicked, this, &QQNavigationBar::userInfoCardClicked);
	connect(mNavigationView, &QQNavigationView::clicked, this, &QQNavigationBar::onNavigationViewClicked);
	connect(mFooterView, &QQNavigationView::clicked, this, &QQNavigationBar::onFooterViewClicked);
}

QQNavigationBar::~QQNavigationBar()
{
}

void QQNavigationBar::setUserInfoCardVisible(bool isVisible)
{
	mUserButton->setVisible(isVisible);
}

void QQNavigationBar::setUserInfoCardPixmap(QPixmap pixmap)
{
	mUserButton->setPixmap(pixmap);
}

void QQNavigationBar::setUserInfoCardSize(QSize size)
{
	mUserButton->setFixedSize(size);
}

void QQNavigationBar::setNavigationIconSize(QSize size)
{
	mNavigationDelegate->setItemSize(size);
}

void QQNavigationBar::setFooterIconSize(QSize size)
{
	mFooterDelegate->setItemSize(size);
}

QString QQNavigationBar::addPageNode(QWidget* page, QIcon icon)
{
	QString pageKey = mNavigationModel->addPageNode(icon);
	if (page)
	{
		page->setProperty("QQPageKey", pageKey);
	}
	return pageKey;
}

QString QQNavigationBar::addPageNode(QWidget* page, int keyPoints, QIcon icon)
{
	QString pageKey = mNavigationModel->addPageNode(keyPoints, icon);
	if (page)
	{
		page->setProperty("QQPageKey", pageKey);
	}
	return pageKey;
}

QString QQNavigationBar::addFooterNode(int keyPoints, QIcon icon)
{
	return addFooterNode(nullptr, keyPoints, icon);
}

QString QQNavigationBar::addFooterNode(QWidget* page, int keyPoints, QIcon icon)
{
	QString footerKey = mFooterModel->addPageNode(keyPoints, icon);
	if (page)
	{
		page->setProperty("QQPageKey", footerKey);
	}
	return footerKey;
}

void QQNavigationBar::setNodeKeyPoints(QString nodeKey, int keyPoints)
{
	QQNavigationNode* node = mNavigationModel->getNavigationNode(nodeKey);
	node->setKeyPoints(keyPoints);
}

int QQNavigationBar::getNodeKeyPoints(QString nodeKey) const
{
	QQNavigationNode* node = mNavigationModel->getNavigationNode(nodeKey);
	return node->getKeyPoints();
}

void QQNavigationBar::onNavigationViewClicked(const QModelIndex& index)
{
	QQNavigationNode* node = index.data(Qt::UserRole).value<QQNavigationNode*>();
	if (node->getKeyPoints())
	{
		node->setKeyPoints(0);
		mNavigationView->update();
	}

	QQNavigationNode* selectedNode = mNavigationModel->getSelectedNode();
	if (selectedNode != node)
	{
		emit navigationNodeClicked(node->getNodeKey());
		if (mNavigationModel->getSelectedNode())
		{
			mNavigationView->clearSelection();
			mNavigationModel->setSelectedNode(nullptr);
		}
		mNavigationView->clearSelection();
		mNavigationView->selectionModel()->select(index, QItemSelectionModel::Select);
		mNavigationModel->setSelectedNode(node);
	}
}

void QQNavigationBar::onFooterViewClicked(const QModelIndex& index)
{
	QQNavigationNode* node = index.data(Qt::UserRole).value<QQNavigationNode*>();
	if (node->getKeyPoints())
	{
		node->setKeyPoints(0);
		mFooterView->update();
	}

	QQNavigationNode* selectedNode = mFooterModel->getSelectedNode();
	if (selectedNode != node)
	{
		emit navigationNodeClicked(node->getNodeKey());
		if (mFooterModel->getSelectedNode())
		{
			mFooterView->clearSelection();
			mFooterModel->setSelectedNode(nullptr);
		}
		mFooterView->clearSelection();
		mFooterView->selectionModel()->select(index, QItemSelectionModel::Select);
		mFooterModel->setSelectedNode(node);
	}
}
